//
//  AppDelegate.m
//  Travels
//
//  Created by 郭明皓 on 15/10/21.
//  Copyright (c) 2015年 郭明皓工作室. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "SurroundViewController.h"
#import "FindViewController.h"
#import "MyController.h"
#import "leadViewController.h"
#import "UMSocial.h"
#import "DKNightVersion.h"
#define appKey @"5b2655c71290"
#import "GMH_Share.h"
#import <AFNetworking.h>
#define appSecret @"55988074b9a3faadffa6f74cd3ae7845"
#define WIDTH self.window.frame.size.width
#define HEIGHT self.window.frame.size.height
#import "GMH_Share.h"
@interface AppDelegate ()<UIScrollViewDelegate>

@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain) UIPageControl *page;


@end

@implementation AppDelegate
- (void)dealloc
{
    [_page release];
    [_scrollView release];
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //分享代码钥匙
    [UMSocialData setAppKey:@"5639a9fd67e58e5fad00119c"];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    

    
//    引导页
    NSString *leaderPage = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *leaderPath = [leaderPage stringByAppendingPathComponent:@"leaderPages.txt"];
    NSString *leader = [NSString stringWithContentsOfFile:leaderPath encoding:NSUTF8StringEncoding error:nil];

    if (leader.length == 0) {
        NSString *str = @"is opened";
        [str writeToFile:leaderPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

        self.scrollView = [[UIScrollView alloc] initWithFrame:self.window.frame];
        self.scrollView.backgroundColor = [UIColor whiteColor];
        [self.window addSubview:self.scrollView];
        [_scrollView release];
        
        self.scrollView.contentSize = CGSizeMake(3 * self.window.frame.size.width, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointMake(0 , 0);
        self.scrollView.delegate = self;
        
        for (int i = 1; i < 4; i++) {
            NSString *imageName = [NSString stringWithFormat:@"leader%d.jpg", i];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i - 1) * self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height)];
            imageView.image = [UIImage imageNamed:imageName];
            [self.scrollView addSubview:imageView];
            [imageView release];
        }
        
        self.scrollView.bounces = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        
        
        // 创建一个pageControl
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 4, HEIGHT / 8 * 7 , WIDTH / 2, HEIGHT / 10)];
        [self.window addSubview:self.page];
        
        self.page.numberOfPages = 3;  // 点的个数
        self.page.currentPageIndicatorTintColor = [UIColor blackColor];
        self.page.pageIndicatorTintColor = [UIColor grayColor];
        [_page release];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"开始体验" forState:(UIControlStateNormal)];
        button.backgroundColor = [UIColor whiteColor];
        button.frame = CGRectMake(self.window.frame.size.width * 2 + WIDTH / 4, HEIGHT / 5 * 4, WIDTH / 2, HEIGHT / 20);
        [self.scrollView addSubview:button];
        [button addTarget:self action:@selector(clickbutton:) forControlEvents:(UIControlEventTouchUpInside)];
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 10;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor whiteColor].CGColor;
        button.backgroundColor = [UIColor clearColor];

        


    }
    else
    {
        [self create];
//        [self createNetWork];
    }

    

    return YES;
}
- (void)clickbutton:(UIButton *)button
{
    [self create];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.page.currentPage = scrollView.contentOffset.x / self.window.frame.size.width;
    
}
- (void)create
{
    ViewController *root = [[ViewController alloc]init];
    UINavigationController *rootvc = [[UINavigationController alloc]initWithRootViewController:root];
    rootvc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"活动" image:[UIImage imageNamed:@"activity.png"] tag:1000];
    
    SurroundViewController *second = [[SurroundViewController alloc]init];
    UINavigationController *secondVC = [[UINavigationController alloc]initWithRootViewController:second];
    secondVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"周边" image:[UIImage imageNamed:@"surround.png"] tag:1002];
    
    FindViewController *find = [[FindViewController alloc]init];
    UINavigationController *findVC = [[UINavigationController alloc]initWithRootViewController:find];
    findVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"path.png"] tag:1003];
    
    MyController *my = [[MyController alloc]init];
    UINavigationController *myVC = [[UINavigationController alloc]initWithRootViewController:my];
    myVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"my.png"] tag:1004];
    
    
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab.viewControllers = @[rootvc, secondVC, findVC, myVC];
    _window.rootViewController = tab;

    tab.tabBar.nightBarTintColor = UIColorFromRGB(0x444444);
    [tab release];
    [rootvc release];
    [root release];
}



//- (void) createNetWork {
//    AFNetworkReachabilityManager *manager =  [AFNetworkReachabilityManager sharedManager];
//    [AFNetworkReachabilityManager sharedManager];
//    [manager startMonitoring];
//    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        
//        [self showNetWorkWithStatus:status];
//        
//    }];
//}
//
//- (void) showNetWorkWithStatus:(NSInteger)status {
//    NSString *str;
//    switch (status) {
//        case AFNetworkReachabilityStatusUnknown:
//            str = @"未知网络";
//            break;
//        case AFNetworkReachabilityStatusNotReachable:
//            str = @"网络未连接";
//            break;
//        case AFNetworkReachabilityStatusReachableViaWWAN:
//            str = @"移动蜂窝网络已连接";
//            break;
//        case AFNetworkReachabilityStatusReachableViaWiFi:
//            str = @"wlfi已连接";
//            break;
//        default:
//            break;
//    }
//    NSLog(@"%@", str);
//    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前网络状态" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alert show];
//    
//}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
